export function formateDate(dateValue, rule) {
  let fmt = rule || 'yyyy-MM-dd hh:mm:ss'

  /*if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, date.getFullYear())
    }
    const o = {
      // 'y+': date.getFullYear(),
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    }
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        const val = o[k] + ''
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length == 1 ? val : ('00' + val).substr(val.length)
        )
      }
    }*/
  if (dateValue == null || dateValue == '') return ''
  var date = new Date(dateValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()

  return year + '-' + month + '-' + day + ' ' + date.getHours() + ':' + minutes + ':' + seconds
  //return fmt
}

export function formateYearAndMonth(dateValue) {
  if (dateValue == null || dateValue == '') return ''
  var date = new Date(dateValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1

  return year + '-' + month
}

export function formatePaddingZeroDate(dateValue) {
  if (dateValue == null || dateValue == '') return ''
  var date = new Date(dateValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()

  return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}

export function formatShortDate(cellValue) {
  if (cellValue == null || cellValue == '') return ''
  var date = new Date(cellValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()

  return year + '-' + month + '-' + day
}

export function getTimeDistance(time) {
  const now = new Date()
  const targetTime = new Date(time)
  const distance = now - targetTime

  const minutes = Math.floor(distance / (1000 * 60))

  if (minutes < 1) {
    return '刚刚'
  } else if (minutes < 60) {
    return `${minutes} 分钟前`
  } else if (minutes < 1440) {
    const hours = Math.floor(minutes / 60)
    return `${hours} 小时前`
  } else if (minutes < 43200) {
    const days = Math.floor(minutes / 1440)
    return `${days} 天前`
  } else {
    const months = Math.floor(minutes / 43200)
    return `${months} 个月前`
  }
}

// 计算当前时间是否已经超过目标时间，超过返回false，否则返回还剩多少天，用于计算计算活动倒计时时间
export function checkTimeout(targetTime) {
  const now = new Date()
  const targetDate = new Date(targetTime)

  // 检查是否超时
  if (now > targetDate) {
    return false // 已超时
  } else {
    // 计算距离目标时间还有多少天
    const timeDiff = targetDate - now // 毫秒
    const daysLeft = Math.ceil(timeDiff / (1000 * 60 * 60 * 24)) // 转换为天
    return daysLeft
  }
}
